package Example.Components;

import java.util.Date;
import java.util.Vector;

import org.objectweb.fractal.api.control.BindingController;

import Example.Interfaces.Account;
import Example.Interfaces.AccountOprs;
import Example.Interfaces.Bike;
import Example.Interfaces.BikeOprs;
import Example.Interfaces.CourseOprs;



public class Basket implements Account, Bike, BindingController {
	/*
	 * (non-Javadoc)
	 * @see Interfaces.AccountDialogue#login(int, int)
	 */
	public Object[] login(int userID, String userPassword) {
		System.out.println("basket login");
		Object [] userInforA=null;
		Object [] userInforC=null;
		Object userInfor[]=new Object[10];
		/*
		 * find user information from account
		 */
		userInforA=AccountOprs.checkID(userID, userPassword);
		for (int i = 0; i < userInforA.length; i++) {
			userInfor[i]=userInforA[i];
		}
		/*
		 * find user information from course db
		 */
		userInforC=CourseOprs.startCourse(userID);

		for (int i = 1; i < userInforC.length; i++) {		
			userInfor[i+5]=userInforC[i];
			//System.out.println("ee"+userInfor[i+5]);
		}
		/*for (int i = 0; i < userInfor.length; i++) {
			System.out.println("22"+userInfor[i]);
		}*/
		return userInfor;
	}
	/*
	 * (non-Javadoc)
	 * @see Interfaces.AccountDialogue#pay(int, float)
	 */
	public double pay(int userID, double cost) {
		return AccountOprs.debit(userID, cost);
	}
	/*
	 * (non-Javadoc)
	 * @see Interfaces.BikeDialogue#findBike(java.lang.String, java.lang.String)
	 */
	public Vector findBike(String locationStart, String locationEnd) {
		return BikeOprs.selectBike(locationStart, locationEnd);
	}
	/*
	 * (non-Javadoc)
	 * @see Interfaces.BikeDialogue#rentBike(int, int)
	 */
	public boolean rentB(int bikeID, int userID) {
		return BikeOprs.rentBike(bikeID, userID);
	}
	/*
	 * (non-Javadoc)
	 * @see Interfaces.BikeDialogue#returnBike(int, java.lang.String, float)
	 */
	public double returnB(int userID,int bikeID, String location, double trip) {
		/*long s=System.currentTimeMillis();
   		String k=String.valueOf(s);
   		Date l=new Date(s);
   		System.out.println(l);
		BikeOprs.returnBike(bikeID, location,trip);
		Date returnB = dates[1];
		Date rentB = dates[0];
		long lreturn = returnB.getTime();
		long lrent = rentB.getTime();
		long difference = lreturn-lrent;
		double cost =1*(double)difference/1000;
		*/
		/*
		 * update user and bike information
		 */
		BikeOprs.returnBike(bikeID, location, trip);
		String rentT=CourseOprs.endCourse(userID,bikeID);

		long t1=Long.parseLong(rentT);
		long t2=System.currentTimeMillis();
		double t=(t2-t1)/1000/60*0.1;
		return 0.0;
	}

	
	//the component interfaces required
	public String[] listFc() { return new String[]{"AccountOprs,BikeOprs,CourseOprs"};}
	
   	public Object lookupFc(String cItf) {
   	if (cItf.equals("CourseOprs"))
   		return CourseOprs;
   	if (cItf.equals("BikeOprs")) 
   		return BikeOprs;
   	if (cItf.equals("AccountOprs")) 
   		return AccountOprs;
   	return null; 
   	}
   	
   	public void bindFc(String cItf, Object sItf)
   	{ 	
   	   	if (cItf.equals("CourseOprs"))
   	   		CourseOprs=(CourseOprs)sItf;
   	   	if (cItf.equals("BikeOprs")) 
   	   		BikeOprs=(BikeOprs)sItf;
   	   	if (cItf.equals("AccountOprs")) 
   	   		AccountOprs=(AccountOprs)sItf;
   	}
   	
   	public void unbindFc(String cItf)
   	{ 
   	   	if (cItf.equals("CourseOprs"))
   	   		CourseOprs=null;
   	   	if (cItf.equals("BikeOprs")) 
   	   		BikeOprs=null;
   	   	if (cItf.equals("AccountOprs")) 
   	   		AccountOprs=null;
   	}
   	
   	private AccountOprs AccountOprs;
   	private CourseOprs CourseOprs;
   	private BikeOprs BikeOprs;

	
}
